package 多线程.三个线程实现顺序打印;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 除了Object的wait和notify/notify可以实现等待/通知机制,Condition和Lock配合同样可以完成等待通知机制
 * 使用condition.await(),使当前线程进入等待状态,使用condition.signal()或者condition.signalAll()唤醒等待线程
 *
 * 这个自己用类似于synchronized的方式使用signalAll()方法再实现一下
 */
public class Test4 {
    private final static Lock lock = new ReentrantLock();
    //判断是否执行：1表示应该A执行，2表示应该B执行，3表示应该C执行
    private static int state = 1;
    private static Condition a = lock.newCondition();
    private static Condition b = lock.newCondition();
    private static Condition c = lock.newCondition();

    public static void main(String[] args) {
        Test4 t = new Test4();
        Thread t1 = new Thread() {
            @Override
            public void run() {
                t.printA();
            }
        };
        Thread t2 = new Thread() {
            @Override
            public void run() {
                try {
                    t.printB();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        Thread t3 = new Thread() {
            @Override
            public void run() {
                try {
                    t.printC();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        t1.start();
        t2.start();
        t3.start();
    }

    public static void printA() {
        //通过循环，hang住线程
        while (state < 4) {
            try {
                lock.lock();
                //并发情况下，不能用if，要用循环判断等待条件，避免虚假唤醒
                while (state != 1) {
                    a.await();
                }
                System.out.println("A");
                state++;
                b.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();//释放锁
            }
        }
    }

    public static void printB() throws InterruptedException{
        while (state < 4) {
            try {
                lock.lock();
                while (state != 2) {
                    b.await();
                }
                //获取到锁，应该执行
                System.out.println("B");
                state++;
                c.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();//释放锁
            }
        }
    }

    public static void printC()  throws InterruptedException{
        while (state < 4) {
            try {
                lock.lock();
                while (state != 3) {
                    c.await();
                }
                //获取到锁，应该执行
                System.out.println("C");
                state++;
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();//释放锁
            }
        }
    }
}
